<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气案例</title>
    <script src="../js/vue.js"></script>
</head>
<body>

    <!-- 
        监视属性watch:
            1、当监视的属性变化时，回调函数自动调用，进行相关操作
            2、监视的属性必须存在，才能进行监视；
            3、监视的两种方法
                （1）new Vue时传入watch配置
                （2）通过vm.$watch()配置
     -->

    <div id="root">
        <h2>今天天气很{{info}}</h2>
        <button @click="isHot = !isHot">变天气1</button><br>
        <button @click="changeWeather">变天气2</button><br>
        <hr>
        <h3>a的值是:{{numbers.a}}</h3>
        <button @click="numbers.a++">点击a+1</button>
        <h3>b的值是:{{numbers.b}}</h3>
        <button @click="numbers.b++">点击b+1</button>
    </div>
    <script>
        Vue.config.productionTip = false

        const vm = new Vue({
            el: '#root',
            data: {
                isHot: true,
                numbers: {
                    a: 1,
                    b: 2
                }
            },
            computed: {
                info() {
                    return this.isHot ? '炎热' : '凉爽'
                }
            },
            methods: {
                changeWeather() {
                    this.isHot = !this.isHot
                }
            },
            watch: {
                isHot: {
                    immediate: true,
                    handler(newV, oldV) {
                        console.log('isHot被改变', newV, oldV);
                    }
                },
                'numbers.a': {
                    handler() {
                        console.log('a值改变了')
                    }
                },
                numbers: {
                    // 监视多层级变化
                    deep: true,
                    handler(newV, oldV) {
                        console.log('numbers改变了', newV, oldV)
                    }
                }
            }
        })
    </script>
</body>
</html>